import {createStore, applyMiddleware, compose, combineReducers} from 'redux';
import createHashHistory from 'history/createHashHistory';
import {Middlewares, Reducers} from 'fe-lib';
import reducers from './reducers';
import DevTools from './DevTools';

const history = createHashHistory();
const routerMiddleware = Middlewares.routerMiddleware(history);
let enhancer = applyMiddleware(routerMiddleware, ...Middlewares.middlewares);
if (__DEV__) {
  // console error in development environment
  Middlewares.errorMiddleware.isConsole = true;

  enhancer = compose(
    enhancer,
    enhancer,
    DevTools.instrument()
  );
}
const rootReducers = combineReducers({
  error: Reducers.error,
  loading: Reducers.loading,
  ...reducers,
});

const store = createStore(rootReducers, enhancer);

if (module.hot) {
  // Enable webpack hot module replacement for reducers
  module.hot.accept(
    // ['./form/reducers', './workflow/reducers'],
    './reducers',
    () => store.replaceReducer(rootReducers)
  );
}


export default store;
